// 插入话题的自定义菜单
import { IDomEditor, IButtonMenu } from '@wangeditor/editor';
import { isMentionDisabled } from '../../isDisabledMenusConfig';
import { TOPIC_ICON_SVG } from '../../icon-svg';
import { getMentionConfig } from '../insertMention';

// 引入 mitt
class InsertTopic implements IButtonMenu {
  tag: string;
  title: string;
  iconSvg: string;
  constructor() {
    this.title = '插入话题';
    this.iconSvg = TOPIC_ICON_SVG;
    this.tag = 'button';
  }

  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    // TS 语法
    return false;
  }

  // 获取菜单执行时的 value ，用不到则返回空 字符串或 false
  getValue(editor: IDomEditor): string | boolean {
    // TS 语法
    return '';
  }

  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    return isMentionDisabled(editor);
  }

  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;
    // 还原选区
    // editor.emit('insertTopic');
    const { showTopicModal, hideTopicModal } = getMentionConfig(editor);
    showTopicModal('click',editor);
  }
}

export default InsertTopic;
